package com.example.demo.service;

import com.example.demo.domain.User;
import com.example.demo.domain.UserExample;
import com.example.demo.exception.BusinessException;
import com.example.demo.exception.BusinessExceptionCode;
import com.example.demo.mapper.UserMapper;
import com.example.demo.req.UserLoginReq;
import com.example.demo.req.UserQueryReq;
import com.example.demo.req.UserResetPasswordReq;
import com.example.demo.req.UserSaveReq;
import com.example.demo.resp.PageResp;
import com.example.demo.resp.UserLoginResp;
import com.example.demo.resp.UserQueryResp;
import com.example.demo.util.CopyUtil;
import com.example.demo.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class UserService {

    private static final Logger LOG = (Logger) LoggerFactory.getLogger(UserService.class);
    @Resource
    private UserMapper userMapper;

    /**
     * @Resource  是jdk自带 @Autowired是Spring自带的
     * */
    @Resource
    private SnowFlake snowFlake;

    public PageResp<UserQueryResp> list(UserQueryReq req){

        UserExample userExample=new UserExample();
        UserExample.Criteria criteria= userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getLoginName())) {
            criteria.andNameLike( req.getLoginName());
        }

        PageHelper.startPage(req.getPage(),req.getSize());
        List<User>userList= userMapper.selectByExample(userExample);

        PageInfo<User> pageInfo = new PageInfo<>(userList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());


        //列表复制
        List<UserQueryResp> list=CopyUtil.copyList(userList, UserQueryResp.class);
        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageResp.getTotal());
        pageResp.setList(list);
        return pageResp;
    }

    /**
     * 保存
    * */
    public void save(UserSaveReq req){
        User user=CopyUtil.copy(req,User.class);
        if(ObjectUtils.isEmpty(req.getId())){
            if(ObjectUtils.isEmpty(selectByLoginName(req.getLoginName()))){
                //新增
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else {
                //用户名已存在
                throw  new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);

            }

        }else{
            //更新
            user.setLoginName(null);
            user.setPassword(null);
            userMapper.updateByPrimaryKeySelective(user);
        }

    }
    public void  delete(Long id){
        userMapper.deleteByPrimaryKey(id);
    }


    public User selectByLoginName(String LoginName){
        UserExample userExample=new UserExample();
        UserExample.Criteria criteria= userExample.createCriteria();
        criteria.andLoginNameEqualTo(LoginName);
        List<User>userList= userMapper.selectByExample(userExample);
        if(CollectionUtils.isEmpty(userList)){
            return  null;
        }else {
            return userList.get(0);
        }

    }

    /**
     * 修改密码
     * */
    public void resetPassword(UserResetPasswordReq req){
        User user=CopyUtil.copy(req,User.class);
        userMapper.updateByPrimaryKeySelective(user);


    }
    /**
     * 登录
     * */
    public UserLoginResp login(UserLoginReq req){
        User userbd=selectByLoginName(req.getLoginName());
        if(ObjectUtils.isEmpty(userbd)){
            LOG.info("用戶名不存在,{}",req.getLoginName());
            //用户名不存在
            throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
        }else{
            if(userbd.getPassword().equals(req.getPassword())){
                //登录成功
                UserLoginResp userLoginResp=CopyUtil.copy(userbd, UserLoginResp.class);
                return userLoginResp;
            }else {
                //密碼不對
                LOG.info("密碼不對，輸入密碼：{}，数据库密码；{}",req.getPassword(),userbd.getPassword());

                throw  new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);

            }
        }


    }


}
